<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page errorPage = "/common/error_page.jsp" %>
<%@ page import="com.gpki.gpkiapi.cert.*" %>
<%@ page import="com.gpki.gpkiapi.cms.*" %>
<%@ page import="com.gpki.gpkiapi.util.*" %>
<%@ page import="com.dsjdf.jdf.Logger" %>
<%@ page import="gcc.GccLdapException" %>
<%@ page import="gcc.GccLdapUsr" %>
<%@ page import="com.welfare.common.*" %>
<%@ page import="com.zll.framework.model.*" %>
<%@ page import="java.util.*" %>

<%@ include file='./gpkisecureweb.jsp'%>
<%

	////////////////////////// GPKI ///////////////////////////////
	
	X509Certificate cert = null; 
	byte[] signData = null;
	byte[] privatekey_random = null;
	String signType = "";
	String queryString = "";
//	GPKIHttpServletRequest gpkirequest = new GPKIHttpServletRequest(request);
          
    System.out.println("STEP .1");
	cert = gpkirequest.getSignerCert(); 
	String subDN = cert.getSubjectDN();

	java.math.BigInteger b = cert.getSerialNumber();
	b.toString();
System.out.println("STEP .2");
	int message_type =  gpkirequest.getRequestMessageType();
 
	if( message_type == gpkirequest.ENCRYPTED_SIGNDATA || message_type == gpkirequest.LOGIN_ENVELOP_SIGN_DATA ||
		message_type == gpkirequest.ENVELOP_SIGNDATA || message_type == gpkirequest.SIGNED_DATA){
		signData = gpkirequest.getSignedData();
		//if(privatekey_random != null) {
     		privatekey_random   = gpkirequest.getSignerRValue();
  		//}
		signType = gpkirequest.getSignType();
	}       
 System.out.println("STEP .3");
	queryString = gpkirequest.getQueryString();
            /* VID 검증
            try{
	            	String ssn = gpkirequest.getParameter("ssn");
		            //if(null != ssn && !"".equals("ssn") && privatekey_random != null){
		            	cert.verifyVID(ssn,privatekey_random);
		            //}
            }catch(Exception e){
            	e.printStackTrace();
            	out.print(e.getMessage());
            }
			*/


	

	////////////////////////// LDAP ///////////////////////////////
	System.out.println("STEP .4");
	GccLdapUsr usr = new GccLdapUsr();
	System.out.println("STEP .5");
	usr.setBaseDN("ou=서울특별시,o=Government of Korea,c=KR");
	usr.setScope(2);
	
	//String cn ="049홍길동001";	// 조회할 사용자 CN
	String cn = subDN.substring(3, subDN.indexOf(','));

    System.out.println("STEP .6");
	Hashtable ht = usr.searchByCN(cn);  //cn값으로만 검색가능cd .
	
	// 개별 정보를 가져오고 싶을 경우
	String user_name = (String) ht.get("displayName");	// 사용자 이름
	String dept_name = (String) ht.get("ou");	// 소속 부서명
	String dept_code = (String) ht.get("ouCode");	// 소속 부서 코드
			
	/*
	// 전체 항목 전부를 조회하고자 할 경우
	Enumeration keys = ht.keys();
	while (keys.hasMoreElements()) {
	    // Key(어트리뷰트명) 를 찾고 찾은 key로 Value를 찾습니다.
	    String key = (String) keys.nextElement();
	    System.out.println("Key(어트리뷰트명) : "+ key +" / 값 : "+ ht.get(key));
	}	
	*/
			
			
			
	////////////////////////// 부서권한 ///////////////////////////////
			
	CommonDAO dao 		= new CommonDAO();

	HashMap infos = new HashMap();
	infos.put("dept_code", dept_code);
	HashList list = dao.selectDeptAuthList(infos);
	
	if (list.size() == 1) {
		session.setAttribute("session.user_id", 	list.get("dept_name",0) + " " + user_name);
		session.setAttribute("session.user_name", 	user_name);
		session.setAttribute("session.dept_name", 	list.get("dept_name",0));
		session.setAttribute("session.user_auth",   list.get("auth_code", 0));
		
		response.sendRedirect("./main.jsp");
	}
	else {
		HashMap result = new HashMap();
		result.put("result", "true");
		result.put("message", "사용권한이 없습니다.");
		result.put("action", "/admin/");
		
		request.setAttribute("result", result);
		
		RequestDispatcher rd = getServletContext().getRequestDispatcher("/common/move_view.jsp");
		rd.forward(request, response);
	}
			
			
			
		
			
%>